package com.zhichat.web.controller;

import com.zhichat.service.ContactInfoService;
import com.zhichat.web.api.R;
import com.zhichat.web.dto.ContactApplyDTO;
import com.zhichat.web.dto.DelContactDTO;
import com.zhichat.web.vo.ContactItemVO;
import com.zhichat.web.vo.UserInfoVO;
import com.zhichat.web.vo.UserSearchVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 联系人功能
 * @Author: 团长九哥 https://space.bilibili.com/602197686
 * @CreateDate: 2024/11/17 13:28
 */
@Slf4j
@Validated
@AllArgsConstructor
@RestController
@RequestMapping("/contact")
public class ContactController {

    private final ContactInfoService contactInfoService;

    /**
     * 根据 contactId 查询联系人（精准查询）
     */
    @GetMapping("/search")
    public R<UserSearchVO> search(@RequestParam(value = "contactId", required = false) @NotBlank(message = "联系人ID必须指定") String contactId) {
        return R.success(contactInfoService.search(contactId));
    }

    /**
     * 联系人（用户、群组）添加/入群 申请
     */
    @PostMapping("/apply")
    public R<Integer> apply(@Validated @RequestBody ContactApplyDTO contactApplyDTO) {
        return R.success(contactInfoService.apply(contactApplyDTO));
    }

    /**
     * 根据传入的类型（用户/群组） 获取联系人List
     */
    @GetMapping("/list")
    public R<List<ContactItemVO>> contactList(@RequestParam("type") String type) {
        return R.success(contactInfoService.contactList(type));
    }

    /**
     * 获取 用户联系人信息
     */
    @GetMapping("/userDetail")
    public R<UserInfoVO> userDetail(@RequestParam(value = "userId", required = false) @NotNull(message = "联系人ID必须指定") Long userId) {
        return R.success(contactInfoService.userDetail(userId));
    }

    /**
     * 删除 联系人
     */
    @PostMapping("/del")
    public R<String> delContact(@Validated @RequestBody DelContactDTO delContactDTO) {
        return R.status(contactInfoService.delContact(delContactDTO));
    }

    /**
     * 拉黑 联系人
     */
    @PostMapping("/blackout")
    public R<String> blackoutContact(@Validated @RequestBody DelContactDTO delContactDTO) {
        return R.status(contactInfoService.blackoutContact(delContactDTO));
    }
}
